/**
 * @file skoll.actions.content.user.js
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: skoll.actions.content.user.js 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/skins/Skoll/js/skoll.actions.content.user.js $
 */

Skoll.actions.content.user = {
	//
	// Public methods.
	login: function() {
		Skoll.tools.preventDefault();
		Skoll.tools.Loading.start();
		var serviceUrl = "service.php?service=users:login" + Skoll.tools.extraUrlParams();
		Skoll.tools.log("Skoll.actions.content.user.login(): URL '" + serviceUrl + "'");
		$.ajax({
			dataType: "json",
			url: serviceUrl,
			type: "POST",
			data: {
				params: JSON.stringify({
					username: $("#username").val(),
					password: $("#password").val(),
					keepme: $("#rememberme:checked").length !== 0
				})
			},
			success: function(data) {
				if (data.status) {
					Skoll.tools.log("Login success");
					Skoll.tools.reload();
				} else {
					Skoll.tools.log("Login error: [" + data.error.code + "] " + data.error.message);
					alert("[" + data.error.code + "] " + data.error.message);
					Skoll.tools.Loading.stop();
				}
			},
			error: function(data) {
				Skoll.tools.log("Login error: " + data);
			}
		});
	},
	logout: function() {
		Skoll.tools.preventDefault();
		Skoll.tools.Loading.start();
		var serviceUrl = "service.php?service=users:logout" + Skoll.tools.extraUrlParams();
		Skoll.tools.log("Skoll.actions.content.user.logout(): URL '" + serviceUrl + "'");
		$.ajax({
			dataType: "json",
			url: serviceUrl,
			type: "POST",
			data: {
				params: JSON.stringify({})
			},
			success: function(data) {
				if (data.status) {
					Skoll.tools.log("Logout success");
					Skoll.tools.reloadHome();
				} else {
					Skoll.tools.log("Login error: [" + data.error.code + "] " + data.error.message);
					alert("[" + data.error.code + "] " + data.error.message);
					Skoll.tools.Loading.stop();
				}
			},
			error: function(data) {
				Skoll.tools.log("Login error: " + data);
			}
		});
	},
	//
	// Protected methods.
	_binds: function() {
	}
}
$(document).ready(function() {
	Skoll.actions.content.user._binds();
});